const path = require("path");
const {CleanWebpackPlugin} = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StringReplaceLoader = require.resolve('./webpack/string-replace-loader.js');
const TemplateRawLoader = require.resolve('./webpack/template-raw-loader.js');

module.exports = {
    mode: 'production',
    entry: {
        loader: "./src/loader.ts",
        plugins: "./src/plugins.ts",
        views: './src/views.tsx',
    },

    // 指定打包文件所在目录
    output: {
        path: path.resolve(__dirname, "release"),
        filename: '[name].min.js',
        library: "[name]",
        libraryTarget: "window",
        clean: true, // 自动将上次打包目录资源清空
        environment: {
            arrowFunction: false,
        },
    },

    // 用来设置引用模块
    resolve: {
        extensions: [".ts", ".tsx", ".js", ".jsx"],
    },

    // 配置webpack的loader
    module: {
        rules: [
            {
                test: /.tsx?$/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    '@babel/preset-env',
                                    {
                                        modules: false,
                                        useBuiltIns: 'usage',
                                        corejs: {
                                            version: 3,
                                            proposals: true
                                        }
                                    }
                                ],
                                '@babel/preset-react'
                            ],
                            plugins: [
                                ['@babel/plugin-proposal-decorators', {legacy: true}],
                                ['@babel/plugin-proposal-class-properties'],
                                ['babel-plugin-import', {
                                    libraryName: 'antd',
                                    libraryDirectory: 'es',
                                    style: true
                                }]
                            ]
                        }
                    },
                    {
                        loader: "ts-loader",
                    },
                    {
                        loader: StringReplaceLoader,
                        options: {
                            TIME_VERSION: new Date().getTime(),
                        },
                    },
                ],
                exclude: /node_modules/,
            },
            {
                test: /.jsx?$/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    '@babel/preset-env',
                                    {
                                        modules: false,
                                        useBuiltIns: 'usage',
                                        corejs: {
                                            version: 3,
                                            proposals: true
                                        }
                                    }
                                ],
                                '@babel/preset-react'
                            ],
                            plugins: [
                                ['@babel/plugin-proposal-decorators', {legacy: true}],
                                ['@babel/plugin-proposal-class-properties'],
                                ['babel-plugin-import', {
                                    libraryName: 'antd',
                                    libraryDirectory: 'es',
                                    style: true
                                }]
                            ]
                        }
                    },
                    {
                        loader: StringReplaceLoader,
                        options: {
                            TIME_VERSION: new Date().getTime(),
                        },
                    },
                ],
                exclude: /node_modules/,
            },
            {
                test: /\.raw\.html$/,
                use: [TemplateRawLoader]
            },
            {
                test: /\.raw\.css$/i,
                use: [TemplateRawLoader]
            },
            {
                test: /\.raw\.scss$/i,
                use: [TemplateRawLoader, 'sass-loader']
            },
            {
                test: /\.raw\.less$/i,
                use: [TemplateRawLoader, 'less-loader']
            },
            {
                test: /^(?!.*\.raw\.css$).*\.css$/i,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
            },
            {
                test: /^(?!.*\.raw\.s[ac]ss$).*\.s[ac]ss$/i,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
            },
            {
                test: /^(?!.*\.raw\.less$).*\.less$/i,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader']
            },
        ],
    },

    // 配置webpack的插件
    plugins: [
        new CleanWebpackPlugin(),
    ],
};
